//
//  DynDemoViewController.m
//  DynamicsDemo
//
//  Created by ソウザ ラファエル on 7/4/25 H.
//  Copyright (c) 25 Heisei ソウザ ラファエル. All rights reserved.
//

#import "DynDemoViewController.h"

#define VIEW_WIDTH  40
#define VIEW_HEIGHT 40
#define NUM_ROWS 3
#define MAX_FORCE 50

@interface DynDemoViewController ()
@property (nonatomic, readonly) UITapGestureRecognizer * tapRecognizer;
-(void) physicsEnabledViewWasTapped:(UIGestureRecognizer*)recognizer;
@end

@implementation DynDemoViewController

@synthesize animator, behavior1, behavior2;
@synthesize referenceView;
@synthesize motionEffect;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    int numViewsPerRow = (int)(self.referenceView.frame.size.width/VIEW_WIDTH);
    int numRows = NUM_ROWS;
    
    NSMutableArray * viewArray = [NSMutableArray arrayWithCapacity:(numViewsPerRow * numRows)];
    
    for(int i = 0; i < numRows; i++){
        for(int j = 0; j < numViewsPerRow; j++){
            CGRect viewFrame = CGRectMake(j * VIEW_WIDTH , i * VIEW_HEIGHT, VIEW_WIDTH, VIEW_HEIGHT);
            UIImageView * view = [[UIImageView alloc]initWithFrame:viewFrame];
            UIImage * viewImage = [UIImage imageNamed:@"appleSymbol.jpg"];
            view.image = viewImage;
            
            UITapGestureRecognizer * tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                      action:@selector(physicsEnabledViewWasTapped:)];
            
            [view addGestureRecognizer:tapRec];
            view.userInteractionEnabled = YES;
            [self.referenceView addSubview:view];
            [viewArray addObject:view];
        }
    }
    
    self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.referenceView];
    
    self.behavior0 = [[UIGravityBehavior alloc]initWithItems:viewArray];
    
    self.behavior1 = [[UIGravityBehavior alloc]initWithItems:viewArray];
    self.behavior1.xComponent =  0.0f;
    self.behavior1.yComponent = -1.0f;
    
    NSMutableArray*  collisionArray = [viewArray mutableCopy];
    
    self.behavior2 = [[UICollisionBehavior alloc] initWithItems:collisionArray];
    self.behavior2.collisionMode = UICollisionBehaviorModeEverything;
    self.behavior2.translatesReferenceBoundsIntoBoundary = YES;
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)startDynamicAnimation:(id)sender {
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:self.behavior0];
    [self.animator addBehavior:self.behavior2];
}

- (IBAction)startDynamicActionReverse:(id)sender {
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:self.behavior1];
    [self.animator addBehavior:self.behavior2];
}

@synthesize tapRecognizer = _tapRecognizer;


-(void) physicsEnabledViewWasTapped:(UIGestureRecognizer*)recognizer{
    UIView * viewTouched = recognizer.view;
    
    UIPushBehavior * push = [[UIPushBehavior alloc] initWithItems:@[viewTouched] mode:UIPushBehaviorModeInstantaneous];
    push.xComponent = MAX_FORCE*(2.0f*(float)(rand())/RAND_MAX -1);
    push.yComponent = MAX_FORCE*(2.0f*(float)(rand())/RAND_MAX -1);
    
    [animator addBehavior:push];
}

@end
